<?php
/**
 * 站点配置管理
 * @author cjli
 *
 */
class SiteAction extends AdminAction
{
	public function _initialize()
	{
		parent::_initialize();
	}
	
	public function index()
	{
		$this->config();
	}
	
	//网站配置
	public function config()
	{
		$config = SiteModel::getShopConfigList();
		if(isset($_POST['dosubmit'])){
			unset($_POST['mail_to']);
			unset($_POST['dosubmit']);
			SiteModel::updateShopConfig($_POST);
			$tab = isset($_GET['tab']) ? intval($_GET['tab']) : '';
			$this->success('保存成功', U('site/config', $tab ? ('tab='.$tab) : ''));
		}else{
			$languageList = SiteModel::getLanguageList();
			$this->assign('languageList', $languageList);
			$themsList = SiteModel::getThemsList();
			$this->assign('themsList', $themsList);
			$this->assign('config', $config);
		  $this->display('config');
		}
	}
	
	//友情链接列表
	public function link_list()
	{
		
		$pageRows = 20;
		if(isset($_REQUEST['pageRows']) && intval($_REQUEST['pageRows'])){
			$pageRows = intval($_REQUEST['pageRows']);
		}
		$link_db = D('link');
		$count = $link_db->count();//总记录数
		import("ORG.Util.Page");// 导入分页类
		$Page = new Page($count, $pageRows);// 实例化分页类 传入总记录数和每页显示的记录数
		$pageShow = $Page->show();// 分页显示输出
		$this->assign('pageShow', $pageShow);
		
		$link_db->field('id,name,linktype,logo,listorder,istop,disabled');
		$link_db->order('istop DESC, listorder DESC, id ASC');
		$link_db->limit($Page->firstRow.','.$Page->listRows);
		$linkList = $link_db->select();
		$this->assign('linkList', $linkList);
		$this->display('link_list');
	}
	
	//添加友情链接
	public function link_add()
	{
		if(isset($_POST['dosubmit'])){
			$data = $_POST['link'];
			if (isset($_FILES['upimage']) && $_FILES['upimage']['name']){
				$info = $this->upload('link');
				if(is_array($info)) {// 上传成功 获取上传文件信息
					$data['logo'] = UPLOAD_PATH . $info['upimage']['savename'];
				}else{
					$this->error($info);
				}
			}
			$link_db = D('link');
			$linkId = $link_db->add($data);
			if($linkId){
				S('link_list_0', NULL);
				S('link_list_1', NULL);
				$this->success('友情链接添加成功！', U('site/link_list'),'add');
			}else{
				$this->error('友情链接添加失败！', U('site/link_list'), 'add');
			}
		}else{
			$this->display('link_add');
		}
	}
	
	//编辑友情链接
	public function link_edit()
	{
		$linkId = intval($_GET['linkid']);
		$link_db = D('link');
		$linkInfo = $link_db->find($linkId);
		if(!$linkInfo){
			$this->error('友情链接不存在！', U('site/link_list'), 'edit');
		}
		if(isset($_POST['dosubmit'])){
			$data = $_POST['link'];
			if (isset($_FILES['upimage']) && $_FILES['upimage']['name']){
				$info = $this->upload('link');
				if(is_array($info)) {// 上传成功 获取上传文件信息
					$data['logo'] =UPLOAD_PATH . $info['upimage']['savename'];
				}else{
					$this->error($info);
				}
			}
			$link_db = D('link');
			$linkId = $link_db->where('id='.$linkId)->save($data);
			if($linkId){
				S('link_list_0', NULL);
				S('link_list_1', NULL);
				$this->success('友情链接编辑成功！', U('site/link_list'),'edit');
			}else{
				$this->error('友情链接编辑失败！', U('site/link_list'),'edit');
			}
		}else{
			$this->assign('link', $linkInfo);
			$this->display('link_edit');
		}
	}
	
	//友情链接删除
	public function link_delete()
	{
		$linkids = $_POST['linkids'];
		if($linkids){
			$linkid = join(',', $linkids);
		}else{
			$linkid = intval($_GET['linkid']);
		}
		$link_db = D('link');
		$link_db->delete($linkid);
		S('link_list_0', NULL);
		S('link_list_1', NULL);
		$this->success('友情链接删除成功！', U('site/link_list'));
	}
	
	//修改友情链接排序
	public function change_link_listorder_ajax()
	{
		$linkId = intval($_GET['linkid']);
		$listorder = intval($_GET['listorder']);
		$link_db = D('link');
		$link_db->where('id='.$linkId)->save(array('listorder' => $listorder));
		S('link_list_0', NULL);
		S('link_list_1', NULL);
	}
	
	//语言列表
	public function language_list()
	{
		$lange_db = D('language');
		$languageList = $lange_db->select();
		$this->assign('languageList', $languageList);
		$this->display('language_list');
	}
	
	//添加语言
	public function language_add()
	{
		if(isset($_POST['dosubmit'])){
			$lange_db = D('language');
			$lange_db->create();
			$lid = $lange_db->add();
			if($lid){
				S('admin_language_list', NULL);
				$this->success('操作成功！', U('site/language_list'));
			}else{
				$this->error('操作失败！', U('site/language_list'));
			}
		}else{
			$langDirList = SiteModel::getLanguageDirctoryList();
			$this->assign('langDirList', $langDirList);
			$this->display('language_form');
		}
	}
	
	//编辑语言
	public function language_edit()
	{
		$lid = intval($_GET['lid']);
		$lange_db = D('language');
		$language = $lange_db->find($lid);
		if(!$language){
			$this->error('操作不存在！', U('site/language_list'));
		}
		
		if(isset($_POST['dosubmit']))
		{
			$lange_db->create();
			$lid = $lange_db->where('id='.$lid)->save();
			S('admin_language_list', NULL);
			$this->success('操作成功！', U('site/language_list'));
		}else{
			$langDirList = SiteModel::getLanguageDirctoryList();
			$this->assign('langDirList', $langDirList);
			$this->assign('lang', $language);
			$this->display('language_form');
		}
	}
	
	//删除语言
	public function language_delete()
	{
		$lid = intval($_GET['lid']);
		$lange_db = D('language');
		$language = $lange_db->find($lid);
		if(!$language){
			$this->error('操作不存在！', U('site/language_list'));
		}
		$lange_db->delete($lid);
		//跟语言相关的表
		$itemsList = array('article_items','attribute_items','attribute_values_items','category_items','email_template_items','page_items','payment_items','product_items','shipping_items');
		foreach ($itemsList as $model_table_db){
			$model_db = D($model_table_db);
			$model_db->where(array('language_id' => $lid))->delete();
		}
		S('admin_language_list', NULL);
		$this->success('操作成功！', U('site/language_list'));
	}
	
	//多货币管理列表
	public function currency_list()
	{
		$defaultCurrency = C('currency');
		$currency_db = D('currency');
		$currencyList = $currency_db->select();
		foreach ($currencyList as $key => $currency){
			$currencyList[$key]['isdefault'] = ($currency['name'] == $defaultCurrency);
		}
		$this->assign('currencyList', $currencyList);
		$this->display('currency_list');
	}
	
	//货币添加
	public function currency_add()
	{
		if(isset($_POST['dosubmit'])){
			$currency_db = D('currency');
			$currency_db->create();
			$lastId = $currency_db->add();
			if($lastId){
				if(intval($_POST['default']) || is_null(C('currency'))){
					$this->set_default_currency( trim($_POST['name']));
				}
				S('currency_list', NULL);
				$this->success('货币添加成功！', U('site/currency_list'), 'add');
			}else{
				$this->error('货币添加失败！', U('site/currency_list'), 'add');
			}
		}else{
			$this->display('currency_add');
		}
	}
	
	//编辑货币
	public function currency_edit()
	{
		$name = trim($_GET['name']);
		$currency_db = D('currency');
		if(isset($_POST['dosubmit'])){
			$currency_db->create();
			$currency_db->where('name="'.$name.'"')->save();
			if(intval($_POST['default']) || is_null(C('currency'))){
				$this->set_default_currency($name);
			}
			S('currency_list', NULL);
			$this->success('货币编辑成功！', U('site/currency_list'), 'edit');
		}else{
			$currencyInfo = $currency_db->where('name="'.$name.'"')->find();
			$currencyInfo['default'] = ($currencyInfo['name']==C('currency')) ? 1 : 0;
			$this->assign('currency', $currencyInfo);
			$this->display('currency_edit');
		}
	}
	
	//删除货币
	public function currency_delete()
	{
		$name = trim($_GET['name']);
		$currency_db = D('currency');
		$currency_db->where('name="'. $name .'"')->delete();
		S('currency_list', NULL);
		if($name == c('currency')){
			$this->set_default_currency(null);
		}
		$this->success('货币删除成功！', U('site/currency_list'));
	}
	
	//设置默认货币
	private function set_default_currency( $name )
	{
		$config = SiteModel::getShopConfigList();
		SiteModel::updateShopConfig(array('currency' => $name));
	}

	//国家列表
	public function region_list()
	{
		$regionList = D('region')->field('name,id')->select();
		$this->assign('regionList', $regionList);
		$this->display('region_list');
	}
	
	//编辑国家地区
	public function region_edit()
	{
		$regionInfo = array();
		$rid = intval($_GET['rid']);
		$region_db = D('region');
		if($rid){
			$regionInfo = $region_db->find($rid);
		}
		if(isset($_POST['dosubmit'])){
			$region_db->create();
			if($rid){
				$region_db->where('id='.$rid)->save();
			}else{
				$region_db->add();
			}
			S('region_list', null);
			S('country_list', null);
			$this->success('操作成功！', U('site/region_list'));
		}else{
			$this->assign('regionInfo', $regionInfo);
			$this->display('region_edit');
		}
	}
	
	public function region_delete()
	{
		$rid = intval($_GET['rid']);
		$region_db = D('region');
		$region_db->delete($rid);
		S('region_list', null);
		S('country_list', null);
		$this->success('操作成功！', U('site/region_list'));
	}
	
	
	
	//邮件测试
	public function public_test_mail()
	{
		$subject = C('site_name') .' test mail';
		$message = 'this is a test mail from ' . C('site_name');
//		$mailset= Array (
//			'mail_server' => $_POST['mail_server'],
//			'mail_port' => intval($_POST['mail_port']),
//			'mail_type' => intval($_POST['mail_type']),
//			'mail_from' => $_POST['mail_from'],
//			'mail_auth' => intval($_POST['mail_auth']),
//			'mail_user' => $_POST['mail_user'],
//			'mail_password' => $_POST['mail_password']
//		);
//		
			$mailInstance = D(WEB_APP_NAME.'://Email');
			if($mailInstance->sendMail($_POST['mail_to'], $subject, $message)){
			echo '邮件发送成功:'.$_POST['mail_to'];
		} else {
			echo '邮件发送失败';
		}	
	}
	/**
	 * 用于编辑器上传图片
	 */
	Public function public_editor_upload()
	{
		import("ORG.Net.UploadFile");
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath =  '.' . UPLOAD_PATH ;// 设置附件上传目录
		$upload->autoSub = true;//开启子目录
		$upload->subType = 'custom';// 子目录创建方式 可以使用hash date custom
		$upload->subDir = date('Y/m/d/', time());
		if(!$upload->upload()) {// 上传错误提示错误信息
			$result = array(
	      'err' => '上传错误',
	      'msg' => $upload->getErrorMsg(),
	    );
		}else{// 上传成功 获取上传文件信息
			$info = $upload->getUploadFileInfo();
			$result = array(
	      'err' => '',
	      'msg' => array(
          'id' => 1,
          'url' => UPLOAD_PATH.$info[0]['savename'],
          'localname' => $info[0]['name'],
        ),
	    );
	 	}
	 	echo json_encode($result);exit;
	}
}